var _oServiceRetrieveCommand;
var _oAppointmentRequest;
var _iAnchorOffset = 0;

function SchedulingDialogJsWindowOnLoad()
{
_oServiceRetrieveCommand = new RemoteCommand("Service", "Retrieve");
PopulateForm(window.dialogArguments);
crmGrid.attachEvent("OnRefresh", GridRefreshed);
crmGrid.attachEvent("OnChangePage", PrepareForPageChange);
crmGrid.attachEvent("OnSelectionChange", GridSelectionChanged);
crmForm.UseDefaultDuration.attachEvent("onclick", ResetToDefaultDuration);
crmForm.serviceid.attachEvent("onchange", ServiceLookupChanged);

crmGrid.OnBeforeFormLoad = HandleGridItemSelected;
crmGrid.SetParameter("disableDblClick", "0");

crmForm.detachCloseAlert();
var oServiceid = document.getElementById("serviceid");
if (_bAutoSearch && !IsNull(oServiceid.DataValue))
{
Search();
}
}
window.attachEvent('onload', SchedulingDialogJsWindowOnLoad);
function HandleGridItemSelected()
{

SelectProposal();

event.returnValue = false;
}
function PopulateForm(oActivityInfo)
{

if (IsNull(oActivityInfo))
{
return;
}
var i;
var item;
var aoItems;
var oLookupItem;
if (!IsNull(oActivityInfo.Customers))
{
aoItems = new Array();
for (i = 0; i < oActivityInfo.Customers.length; i++)
{
item = oActivityInfo.Customers[i];

oLookupItem = new LookupControlItem(item.EntityId, item.ObjectTypeCode, item.DisplayName);
aoItems.push(oLookupItem);
}
var customers = document.getElementById("customers");
customers.Clear();
customers.AddItems(aoItems);
}
if (!IsNull(oActivityInfo.Resources))
{
aoItems = new Array();
for (i = 0; i < oActivityInfo.Resources.length; i++)
{
item = oActivityInfo.Resources[i];

oLookupItem = new LookupControlItem(item.EntityId, item.ObjectTypeCode, item.DisplayName);
oLookupItem.resourceSpecId = item.ResourceSpecId;
oLookupItem.effort = item.EffortRequired;
aoItems.push(oLookupItem);
}
var resources = document.getElementById("resources");
resources.Clear();
resources.AddItems(aoItems);
}
if (!IsNull(oActivityInfo.Service))
{
aoItems = new Array();
item = oActivityInfo.Service;

oLookupItem = new LookupControlItem(item.EntityId, item.ObjectTypeCode, item.DisplayName);
aoItems.push(oLookupItem);
var serviceid = document.getElementById("serviceid");
serviceid.Clear();
serviceid.AddItems(aoItems);

var oEvent = new Object();
oEvent.DoNotClearSpecIds = true;
ServiceLookupChanged(oEvent);
}
if (!IsNull(oActivityInfo.Site))
{
aoItems = new Array();
item = oActivityInfo.Site;

oLookupItem = new LookupControlItem(item.EntityId, item.ObjectTypeCode, item.DisplayName);
aoItems.push(oLookupItem);
var siteid = document.getElementById("siteid");
siteid.Clear();
siteid.AddItems(aoItems);
}
}
function SelectProposal()
{
window.returnValue = BuildProposalInfo();
if (window.returnValue == null)
{
alert(LOCID_SCHEDDLG_SELECT_PROPOSAL);
return;
}
window.close();
}

function cancel()
{
window.returnValue = null;
window.close();
}

function BuildProposalInfo()
{
var selectedRecords = crmGrid.InnerGrid.SelectedRecords;
if (selectedRecords.length === 0)
{
return null;
}

var dStart;
var dEnd;
var oService;
var oSite;

var aoCustomers = new Array();
var aoResources = new Array();

var gridBodyTable = crmGrid.all["gridBodyTable"];
var iSelectedIndex = selectedRecords[0][2];
var oRow = crmGrid.InnerGrid.rows[iSelectedIndex];
var i;
var iLen = oRow.cells.length;
var oCellData;
var sProperty;
var oValueSpan;
for (i = 0; i < iLen; i++)
{

oCellData = oRow.cells[i].childNodes[0];

sProperty = gridBodyTable.getElementsByTagName("COL")[i].name;
switch (sProperty)
{
case "Service":
oValueSpan = oCellData.childNodes[0];
oService = new ServiceObject(oValueSpan.innerText, oValueSpan.oid);
break;
case "Site":
oValueSpan = oCellData.childNodes[0];


if (oValueSpan != null)
{
oSite = new SiteObject(oValueSpan.innerText, oValueSpan.oid);
}
break;
case "Start":
dStart = ParseDateTime(oCellData.innerText);
break;
case "End":
dEnd = ParseDateTime(oCellData.innerText);
break;
case "ProposalParties":
var iCount = oCellData.childNodes.length;
for (var j = 0; j < iCount; j++)
{
oValueSpan = oCellData.childNodes[j];

if (oValueSpan.tagName != "SPAN")
{
continue;
}
aoResources.push(new AttendeeObject(Trim(oValueSpan.innerText), oValueSpan.oid, oValueSpan.otype, oValueSpan.resourcespecid, oValueSpan.effort));
}
break;
}
}

var oAttendeeLookup = document.getElementById("customers");
var aoItems = oAttendeeLookup.Items();
for (i = 0; i < aoItems.length; i++)
{
aoCustomers.push(new AttendeeObject(aoItems[i].name, aoItems[i].id, aoItems[i].type));
}
return new ActivityInfo(aoCustomers, aoResources, oService, oSite, dStart, dEnd);
}
function StartTimeChanged()
{
var SearchStartTime = document.getElementById("SearchStartTime");
switch (SearchStartTime.value)
{
case _sSpecificTime:
rangeOfTimesArea.style.display = "none";
specificTimeArea.style.display = "inline";
break;
case _sRangeOfTimes:
rangeOfTimesArea.style.display = "inline";
specificTimeArea.style.display = "none";
break;
case _sAnyTime:
case _sMorning:
case _sAfternoon:
case _sEvening:
rangeOfTimesArea.style.display = "none";
specificTimeArea.style.display = "none";
break;
default:
break;
}
}
function SearchDayOfWeekChanged()
{
var SearchStartDate = document.getElementById("SearchStartDate");
var rangeOfDatesArea = document.getElementById("rangeOfDatesArea");
var rangeOfDatesForDaysArea = document.getElementById("rangeOfDatesForDaysArea");
var specificDateArea = document.getElementById("specificDateArea");

SearchStartDate.value = _sRangeOfDates;
rangeOfDatesForDaysArea.style.display = "inline";
rangeOfDatesArea.style.display = "inline";
specificDateArea.style.display = "none";

SetSearchDayOfWeek(event.Value);
}
function StartDateChanged()
{
var SearchStartDate = document.getElementById("SearchStartDate");
var rangeOfDatesArea = document.getElementById("rangeOfDatesArea");
var rangeOfDatesForDaysArea = document.getElementById("rangeOfDatesForDaysArea");
var specificDateArea = document.getElementById("specificDateArea");
switch (SearchStartDate.value)
{
case _sSpecificDate:
rangeOfDatesForDaysArea.style.display = "none";
rangeOfDatesArea.style.display = "none";
specificDateArea.style.display = "inline";
break;
case _sRangeOfDates:
rangeOfDatesForDaysArea.style.display = "inline";
rangeOfDatesArea.style.display = "inline";
specificDateArea.style.display = "none";
break;
case _sASAP:
case _sToday:
case _sTomorrow:
case _sThisWeek:
case _sNextWeek:
case _sNextMonth:
rangeOfDatesForDaysArea.style.display = "none";
rangeOfDatesArea.style.display = "none";
specificDateArea.style.display = "none";
break;
default:
break;
}
}
function SetSearchDayOfWeek(dayOfWeek)
{
var oCheckBoxAll = crmForm.DayOfWeekAll;
var oCheckBox = oCheckBoxAll;

for (var i = 0; i < 7; i++)
{
if (document.getElementById("DayOfWeek" + i).value == dayOfWeek)
{

if (oCheckBoxAll.checked)
{
oCheckBoxAll.checked = false;
UpdateDaysCheckboxes();
}
oCheckBox = document.getElementById("DayOfWeek" + i);
oCheckBox.checked = true;

break;
}
}
var SearchStartDate = document.getElementById("SearchStartDate");
var rangeOfDatesForDaysArea = document.getElementById("rangeOfDatesForDaysArea");
var rangeOfDatesArea = document.getElementById("rangeOfDatesArea");
var specificDateArea = document.getElementById("specificDateArea");
SearchStartDate.value = "RangeOfDates";
rangeOfDatesForDaysArea.style.display = "inline";
rangeOfDatesArea.style.display = "inline";
specificDateArea.style.display = "none";
}
function UpdateDaysCheckboxes()
{
var oCheckBox;
var bAllDaysChecked = crmForm.DayOfWeekAll.checked;
for (var i = 0; i < 7; i++)
{
oCheckBox = document.getElementById("DayOfWeek" + i);
oCheckBox.checked = bAllDaysChecked;
oCheckBox.disabled = bAllDaysChecked;
}
}
function ShowHideSearchDuration()
{
var SearchDurationMinutes = document.getElementById("SearchDurationMinutes");
SearchDurationMinutes.Disabled = !SearchDurationMinutes.Disabled;
}

function BuildAppointmentRequest(dStart, dEnd, sDirection)
{

if (sDirection != "Backward" && sDirection != "Forward")
{
sDirection = "Forward";
}
var searchStart = GetStartDate();
var searchEnd = GetEndDate();
var iStartMinutes = GetStartMinutes();
var SearchDurationMinutes = document.getElementById("SearchDurationMinutes");
var iDurationMinutes = SearchDurationMinutes.DataValue;

var sServiceId = "";
var serviceid = document.getElementById("serviceid");
var oServiceLookupItems = serviceid.Items();
if (oServiceLookupItems.length > 0)
{
sServiceId = oServiceLookupItems[0].id;
}
if (dStart == null)
{
dStart = searchStart;
}
if (dEnd == null)
{
dEnd = searchEnd;
}
var sSiteId = "";
var siteid = document.getElementById("siteid");
var oSiteLookupItems = siteid.Items();
if (oSiteLookupItems.length > 0)
{
sSiteId = oSiteLookupItems[0].id;
}
var sSearchXml = "<AppointmentRequest>";
if (sServiceId != "")
{
sSearchXml += FormatNode("ServiceId", sServiceId, true);
}
if (sSiteId != "")
{
sSearchXml += FormatNode("Sites", FormatNode("guid", sSiteId), true);
}
sSearchXml += FormatNode("Direction", sDirection, true);
sSearchXml += FormatNode("NumberOfResults", _iRecordsPerPage, true);
sSearchXml += FormatNode("SearchWindowStart", FormatDateTime(dStart), true);
sSearchXml += FormatNode("SearchWindowEnd", FormatDateTime(dEnd), true);
sSearchXml += FormatNode("AnchorOffset", _iAnchorOffset, true);
sSearchXml += FormatNode("UserTimeZoneCode", _sUserTimeZoneCode, true);

if (HasRecurrence())
{
sSearchXml += FormatNode("SearchRecurrenceRule", GetRecurrenceRule(), true);
var recurrenceStart = GetDate(searchStart);
recurrenceStart.setHours(Math.floor(iStartMinutes / 60));
recurrenceStart.setMinutes(iStartMinutes % 60);
sSearchXml += FormatNode("SearchRecurrenceStart", FormatUtcDateTime(recurrenceStart), true);
sSearchXml += FormatNode("RecurrenceDuration", GetRecurrenceDuration(GetDate(searchStart), searchEnd), true);
sSearchXml += FormatNode("RecurrenceTimeZoneCode", _sUserTimeZoneCode, true);
}
if (!crmForm.UseDefaultDuration.checked)
{
sSearchXml += FormatNode("Duration", iDurationMinutes, true);
}
sSearchXml += BuildAppointmentsToIgnore();
sSearchXml += BuildRequiredResources();
sSearchXml += "</AppointmentRequest>";
return sSearchXml;
}
function FormatNode(tag, value, ns)
{
var sNode = "<" + tag;
if (!IsNull(ns))
{
sNode += " xmlns=\"" + _sSchedulingNamespace + "\"";
}
return sNode + ">" + value + "</" + tag + ">";
}
function BuildAppointmentsToIgnore()
{
var sXml = "";
var oResourceLookup = document.getElementById("resources");
var aoItems = oResourceLookup.Items();
if (_sActivityId != "" && aoItems.length > 0)
{
var oResource;
for (i = 0; i < aoItems.length; i++)
{
oResource = aoItems[i];
sXml += "<AppointmentsToIgnore>";
sXml += FormatNode("ResourceId", oResource.id);
sXml += "<Appointments>";
sXml += FormatNode("guid", _sActivityId);
sXml += "</Appointments>";
sXml += "</AppointmentsToIgnore>";
}
sXml = FormatNode("AppointmentsToIgnore", sXml, true);
}
return sXml;
}
function BuildRequiredResources()
{
var sXml = "";
var oResourceLookup = document.getElementById("resources");
var aoItems = oResourceLookup.Items();
if (aoItems.length > 0)
{
var oResource;
for (i = 0; i < aoItems.length; i++)
{
oResource = aoItems[i];
sXml += "<RequiredResource>";
sXml += FormatNode("ResourceId", oResource.id);
if (!IsNull(oResource.resourceSpecId))
{
sXml += FormatNode("ResourceSpecId", oResource.resourceSpecId);
}
sXml += "</RequiredResource>";
}
sXml = FormatNode("RequiredResources", sXml, true);
}
return sXml;
}
function GetRecurrenceRule()
{
var recurrenceString = "";
var SearchStartDate = document.getElementById("SearchStartDate");
if (SearchStartDate.value == _sRangeOfDates)
{

if (!crmForm.DayOfWeekAll.checked)
{

var daysOfWeek = new Array("SU", "MO", "TU", "WE", "TH", "FR", "SA");
var value;

for (var i = 0; i < 7; i++)
{
if (document.getElementById("DayOfWeek" + i).checked)
{
value = parseInt(document.getElementById("DayOfWeek" + i).value, 10);
if (!isNaN(value) && value >= 0 && value < daysOfWeek.length)
{
if (recurrenceString.length > 0)
{
recurrenceString += ",";
}
recurrenceString += daysOfWeek[value];
}
}
}
}
}
recurrenceString = (recurrenceString == "") ? "FREQ=DAILY" : "FREQ=WEEKLY;BYDAY=" + recurrenceString;
return recurrenceString + ";INTERVAL=1";
}
function GetDate(d)
{
return new Date(d.getFullYear(), d.getMonth(), d.getDate());
}
function AddDays(d, addDays)
{
return new Date(d.getFullYear(), d.getMonth(), d.getDate() + addDays);
}
function GetEndDate()
{
var today = LocalDateTimeNow();
var endDate;

var SearchStartDate = document.getElementById("SearchStartDate");
switch (SearchStartDate.value)
{
case _sASAP:

return new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
case _sSpecificDate:
endDate = SearchSpecificDate.DataValue;
return AddDays(endDate, 1);
case _sRangeOfDates:
if (crmForm.DateRangeBeforeCheckBox.checked)
{
endDate = SearchDateBefore.DataValue;
return AddDays(endDate, 1);
}
else
{

return new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
}
break;
case _sToday:
return AddDays(LocalDateTimeNow(), 1);
case _sTomorrow:
return AddDays(LocalDateTimeNow(), 2);
case _sThisWeek:
endDate = GetLastDayOfWeek(LocalDateTimeNow());
return AddDays(endDate, 1);
case _sNextWeek:
var nextWeek = AddDays(LocalDateTimeNow(), 7);
endDate = GetLastDayOfWeek(nextWeek);
return AddDays(endDate, 1);
case _sNextMonth:
return new Date(today.getFullYear(), today.getMonth() + 2, 1);
default:

return new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
}
}
function GetStartDate()
{
var dValue;
var dNow = LocalDateTimeNow();
var SearchStartDate = document.getElementById("SearchStartDate");
switch (SearchStartDate.value)
{
case _sASAP:
case _sToday:
case _sThisWeek:
return dNow;
case _sSpecificDate:
dValue = SearchSpecificDate.DataValue;
return (dValue < dNow) ? dNow : dValue;
case _sRangeOfDates:
dValue = (crmForm.DateRangeAfterCheckBox.checked) ? SearchDateAfter.DataValue : dNow;
return (dValue < dNow) ? dNow : dValue;
case _sTomorrow:
return AddDays(dNow, 1);
case _sNextWeek:
var nextWeek = AddDays(dNow, 7);
return GetFirstDayOfWeek(nextWeek);
case _sNextMonth:
return new Date(dNow.getFullYear(), dNow.getMonth() + 1, 1);
default:

return dNow;
}
}

function GetRecurrenceDuration(searchStart, searchEnd)
{
var iDurationMinutes = GetEndMinutes() - GetStartMinutes();

if (iDurationMinutes <= 0)
{
iDurationMinutes += 1440;
}
return iDurationMinutes;
}
function GetDtStart(d)
{
var minutes = GetStartMinutes();
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), Math.floor(minutes / 60), minutes % 60, 0);
}
function GetStartMinutes()
{
var result = 0;
var value;
var SearchStartTime = document.getElementById("SearchStartTime");
switch (SearchStartTime.value)
{
case _sMorning:
break;
case _sAfternoon:
result = 12 * 60;
break;
case _sEvening:
result = 16 * 60;
break;
case _sSpecificTime:
value = SearchSpecificTime.DataValue;
result = value.getHours() * 60 + value.getMinutes();
break;
case _sRangeOfTimes:
value = SearchTimeAfter.DataValue;
result = value.getHours() * 60 + value.getMinutes();
break;
}
return result;
}
function GetEndMinutes()
{
var result = 24 * 60;
var value;
var SearchStartTime = document.getElementById("SearchStartTime");
switch (SearchStartTime.value)
{
case _sMorning:
result = 12 * 60;
break;
case _sAfternoon:
result = 16 * 60;
break;
case _sEvening:
break;
case _sSpecificTime:
value = SearchSpecificTime.DataValue;
result = value.getHours() * 60 + value.getMinutes();

result++;
break;
case _sRangeOfTimes:
value = SearchTimeBefore.DataValue;
result = value.getHours() * 60 + value.getMinutes();

result++;
break;
}
return result;
}
function HasRecurrence()
{
var SearchStartTime = document.getElementById("SearchStartTime");
if (SearchStartTime.value != _sAnyTime)
{
return true;
}
return HasDayOfWeekRecurrence();
}
function HasDayOfWeekRecurrence()
{
var SearchStartDate = document.getElementById("SearchStartDate");
if (SearchStartDate.value == _sRangeOfDates)
{

if (!crmForm.DayOfWeekAll.checked)
{
for (var i = 0; i < 7; i++)
{

if (document.getElementById("DayOfWeek" + i).checked)
{
return true;
}
}
}
}
return false;
}
function PrepareForPageChange()
{
var sDirection;
var dStart = null;
var dEnd = null;
if (event.NewPageNumber == null || event.NewPageNumber == 1)
{

sDirection = "Forward";

crmGrid.SetParameter("SearchWindowStart", "");
crmGrid.SetParameter("SearchWindowEnd", "");
}
else
{
var iRecordNumber;
if (event.NewPageNumber > event.CurrentPageNumber)
{

iRecordNumber = _iRecordsPerPage - 1;
sDirection = "Forward";
}
else
{

iRecordNumber = 0;
sDirection = "Backward";
}

var gridBodyTable = crmGrid.all["gridBodyTable"];
var oRow = crmGrid.InnerGrid.rows[iRecordNumber];
var iLen = oRow.cells.length;
var oCellData;
var sProperty;

for (var i = 0; i < iLen; i++)
{

oCellData = oRow.cells[i].childNodes[0];
sProperty = gridBodyTable.getElementsByTagName("COL")[i].name;
if (sProperty == "Start")
{
if (sDirection == "Backward")
{
dEnd = ParseDateTime(oCellData.innerText);

dEnd.setSeconds(dEnd.getSeconds() - 1);

crmGrid.SetParameter("SearchWindowStart", "");
crmGrid.SetParameter("SearchWindowEnd", FormatDateTime(dEnd));
}
if (sDirection == "Forward")
{
dStart = ParseDateTime(oCellData.innerText);

dStart.setSeconds(dStart.getSeconds() + 1);

crmGrid.SetParameter("SearchWindowStart", FormatDateTime(dStart));
crmGrid.SetParameter("SearchWindowEnd", "");
}
break;
}
}
}
if (_oAppointmentRequest == null)
{
_oAppointmentRequest = BuildAppointmentRequest(dStart, dEnd, sDirection);
}

crmGrid.SetParameter("AppointmentRequest", _oAppointmentRequest);
}
function Search()
{

_oAppointmentRequest = null;

var SearchButton = document.getElementById("SearchButton");
var CancelSearchButton = document.getElementById("CancelSearchButton");
SearchButton.style.display = "none";
CancelSearchButton.style.display = "block";
if (ValidateSearchCriteria())
{
var serviceid = document.getElementById("serviceid");
var oServiceLookupItems = serviceid.Items();
var sServiceName = "";
if (oServiceLookupItems.length > 0)
{
sServiceName = oServiceLookupItems[0].name;
}
crmGrid.SetParameter("ServiceName", sServiceName);

crmGrid.PageNumber = 1;
}
else
{
GridRefreshed();
}
}

function GridRefreshed()
{

var CancelSearchButton = document.getElementById("CancelSearchButton");
var SearchButton = document.getElementById("SearchButton");
CancelSearchButton.style.display = "none";
SearchButton.style.display = "block";
}
function GridSelectionChanged()
{

BookButton.disabled = (crmGrid.InnerGrid.SelectedRecords.length === 0);
}
function ResetToDefaultDuration()
{
if (crmForm.UseDefaultDuration.checked)
{
var SearchDurationMinutes = document.getElementById("SearchDurationMinutes");
SearchDurationMinutes.ResetToDefault();
}
}
function CancelSearch()
{



if (event.srcElement.id != "CancelSearchButton")
{
var SearchButton = document.getElementById("SearchButton");
window.parent.focus();
SearchButton.focus();
return;
}
BookButton.disabled = true;
crmGrid.CancelRefresh();
GridRefreshed();
}
function ValidateSearchCriteria()
{
var searchEnd;
var dNow;
var serviceid = document.getElementById("serviceid");

if (IsNull(serviceid.DataValue))
{
serviceid.SetFocus();
alert(LOCID_SCHEDDLG_SVCREQUIRED);
return false;
}

var SearchStartDate = document.getElementById("SearchStartDate");
if (SearchStartDate.value == "")
{
SearchStartDate.focus();
alert(LOCID_SCHEDDLG_SELECTSTARTDATE);
return false;
}

var SearchStartTime = document.getElementById("SearchStartTime");
if (SearchStartTime.value == "")
{
SearchStartTime.focus();
alert(LOCID_SCHEDDLG_SELECTSTARTTIME);
return false;
}
if (SearchStartDate.value == _sRangeOfDates)
{

if (!HasDayOfWeekRecurrence() && !crmForm.DayOfWeekAll.checked)
{
crmForm.DayOfWeekAll.focus();
alert(LOCID_SCHEDDLG_SELECTDAYOFWEEK);
return false;
}

if (!crmForm.DateRangeBeforeCheckBox.checked && !crmForm.DateRangeAfterCheckBox.checked)
{
crmForm.DateRangeAfterCheckBox.focus();
alert(LOCID_SCHEDDLG_BEFOREAFTERDTRQD);
return false;
}

if (crmForm.DateRangeAfterCheckBox.checked && SearchDateAfter.DataValue == null)
{
SearchDateAfter.focus();
alert(LOCID_SCHEDDLG_STARTAFTR_RQD);
return false;
}

if (crmForm.DateRangeBeforeCheckBox.checked && SearchDateBefore.DataValue == null)
{
SearchDateBefore.focus();
alert(LOCID_SCHEDDLG_STARTBFOR_RQD);
return false;
}


if (crmForm.DateRangeBeforeCheckBox.checked && crmForm.DateRangeAfterCheckBox.checked)
{
if (SearchDateBefore.DataValue < SearchDateAfter.DataValue)
{
SearchDateAfter.focus();
alert(LOCID_SCHEDDLG_INVALIDDATERANGE);
return false;
}
}

dNow = LocalDateTimeNow();
searchEnd = GetEndDate();
if (searchEnd < dNow)
{
SearchDateAfter.focus();
alert(LOCID_SCHEDDLG_DATEINTHEPAST);
return false;
}
}

if (SearchStartDate.value == _sSpecificDate)
{
dNow = LocalDateTimeNow();
searchEnd = GetEndDate();
if (searchEnd < dNow)
{
SearchSpecificDate.focus();
alert(LOCID_SCHEDDLG_DATEINTHEPAST);
return false;
}
}


if (SearchStartTime.value == _sRangeOfTimes)
{

if (GetStartMinutes() >= GetEndMinutes())
{
SearchTimeAfter.focus();
alert(LOCID_SCHEDDLG_ENDTIMEBFRSTART);
return false;
}
}
var SearchDurationMinutes = document.getElementById("SearchDurationMinutes");
if (!crmForm.UseDefaultDuration.checked && (SearchDurationMinutes.DataValue == null || SearchDurationMinutes.DataValue == 0))
{
SearchDurationMinutesSelect.focus();
alert(LOCID_SCHEDDLG_DURATION_REQUIRED);
return false;
}
return true;
}

function ServiceLookupChanged(oEvent)
{

var serviceid = document.getElementById("serviceid");
var aoItems = serviceid.Items();

if (aoItems.length == 1)
{
var columns = new Array();
columns.type = "string";
columns[0] = "duration";
columns[1] = "anchoroffset";
_oServiceRetrieveCommand.SetParameter("id", aoItems[0].id);
_oServiceRetrieveCommand.SetParameter("columns", columns);
_oServiceRetrieveCommand.Execute(HandleGetService);
}
if (oEvent == null || !oEvent.DoNotClearSpecIds)
{

var oResourcesLookup = document.getElementById("resources");
aoItems = oResourcesLookup.Items();
var oItem;
for (i = 0; i < aoItems.length; i++)
{
oItem = aoItems[i];
oItem.resourceSpecId = null;
oItem.effort = null;
oResourcesLookup.UpdateItem(oItem.id, oItem);
}
}
}
function HandleGetService(oResult)
{
if (oResult.Success)
{
oService = oResult.ReturnValue.service;
var SearchDurationMinutes = document.getElementById("SearchDurationMinutes");

if (crmForm.UseDefaultDuration.checked)
{
SearchDurationMinutes.DataValue = oService.duration;
}
SearchDurationMinutes.ResetDefault(oService.duration);

if (oService.anchoroffset == null)
{
_iAnchorOffset = 0;
}
else
{
_iAnchorOffset = oService.anchoroffset;
}
}
else
{
_iAnchorOffset = 0;
}
}
